﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using GSM.BusinessLayer;
using GSM.BusinessLayer.DataLayer;
using CRM_SMS_MARKETING.EL;
using CRM_SMS_MARKETING.BL;

namespace CRM_SMS_MARKETING.PL.Customer_Care
{
    public partial class frmSpecialDay : DevExpress.XtraEditors.XtraForm
    {
        frmBirthdayCustomer frmBirthdayCustomer;
        public static bool CheckFilter;
        public static List<GSM.BusinessLayer.Customer> ListCustomer;
        List<Event> ListEvent;
        PromotionSql promotionsql;
    
        List<ELNewPromotion> ListVoucher;
        List<ELNewPromotion> ListGift;
        EventSql evensql;
        CustomerSql customersql;
        BLEvent blEvent;
        bool Adding;
        bool Editing;
        public frmSpecialDay()
        {
            InitializeComponent();
            blEvent = new BLEvent();
            CheckFilter = false;
            ListCustomer = new List<GSM.BusinessLayer.Customer>();
            ListEvent = new List<Event>();
            evensql = new EventSql();
            customersql = new CustomerSql();
            promotionsql = new PromotionSql();
            ListVoucher = new List<ELNewPromotion>();
            ListGift = new List<ELNewPromotion>();
            List<SmallPromotion> Voucher = new List<SmallPromotion>();
            List<SmallPromotion> Gift = new List<SmallPromotion>();
            //Lookup Edit

            Voucher = promotionsql.SelectAllSmallPromotionVoucher();
            Gift = promotionsql.SelectAllSmallPromotionGift();

            //Tao gia tri rong cho lookup edit
            blEvent.CreateListVoucherAndGift(Voucher, Gift, ListVoucher, ListGift);
            lookUpEditVoucher.Properties.DataSource = ListVoucher;
            lookUpEditVoucher.Properties.DisplayMember = "PromotionSubject";
            lookUpEditVoucher.Properties.ValueMember = "PromotionSubject";


            lookUpEditGift.Properties.DataSource = ListGift;
            lookUpEditGift.Properties.DisplayMember = "PromotionSubject";
            lookUpEditGift.Properties.ValueMember = "PromotionSubject";
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            Adding = true;
            txtID.Text = ((int)evensql.GetLastReCordID() + 1).ToString();
            txtDateSubject.Text = "";
            txtDescription.Text = "";
            txtGiftDescription.Text = "";
            txtVoucherDescription.Text = "";
            txtSMSFormat.Text = "";
            lookUpEditGift.EditValue = "";
            lookUpEditVoucher.EditValue = "";
            dateEditBegin.EditValue = null;
            dateEditEnd.EditValue = null;
            comboBoxEditField.Text = "";

            #region Enable Control

            gridControlSpecialDay.Enabled = false;
 
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
            btnPrevious.Enabled = false;
            btnNew.Enabled = false;
            btnNext.Enabled = false;
            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
            btnFilter.Enabled = true;
            checkEditStatus.Properties.ReadOnly = false;
            comboBoxEditField.Properties.ReadOnly = false;
            txtDateSubject.Properties.ReadOnly = false;
            txtDescription.Properties.ReadOnly = false;
            txtSMSFormat.Properties.ReadOnly = false;
            lookUpEditVoucher.Properties.ReadOnly = false;
            lookUpEditGift.Properties.ReadOnly = false;
            txtGiftDescription.Properties.ReadOnly = true;
            txtVoucherDescription.Properties.ReadOnly = true;
            groupControlVoucher.Enabled = true;
            groupControlGift.Enabled = true;
            dateEditBegin.Properties.ReadOnly = false;
            dateEditEnd.Properties.ReadOnly = false;
            #endregion
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (gridViewSpecialDay.RowCount != 0)
            {
                Editing = true;
                #region Enable Control
                gridControlSpecialDay.Enabled = false;
                btnEdit.Enabled = false;
                btnNew.Enabled = false;
                btnFilter.Enabled = true;
                btnDelete.Enabled = false;
                btnPrevious.Enabled = false;
                btnNext.Enabled = false;
                btnSave.Enabled = true;
                btnCancel.Enabled = true;
                checkEditStatus.Properties.ReadOnly = false;
                comboBoxEditField.Properties.ReadOnly = false;

                txtDateSubject.Properties.ReadOnly = false;
                txtDescription.Properties.ReadOnly = false;
                txtGiftDescription.Properties.ReadOnly = false;
                lookUpEditVoucher.Properties.ReadOnly = false;
                lookUpEditGift.Properties.ReadOnly = false;
                txtVoucherDescription.Properties.ReadOnly = false;
                txtSMSFormat.Properties.ReadOnly = false;
                dateEditBegin.Properties.ReadOnly = false;
                dateEditEnd.Properties.ReadOnly = false;
                #endregion
            }

        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gridViewSpecialDay.RowCount != 0)
            {
                Event even = new Event();
                bool checkdel = true;
                if (gridViewSpecialDay.RowCount != 0)
                {
                    if (MessageBox.Show("Are you sure to delete?", "Confirm Delete", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                    {
                        try
                        {
                            #region Mapping field
                            even.EventID = Int32.Parse(gridViewSpecialDay.GetFocusedRowCellValue(colID).ToString());
                            even.Subject = txtDateSubject.Text;
                            even.Description = txtDescription.Text;
                            even.Content = txtSMSFormat.Text;
                            even.EventStatus = (bool)checkEditStatus.EditValue;
                            even.BeginDate = (DateTime)dateEditBegin.EditValue;
                            even.FinishDate = (DateTime)dateEditEnd.EditValue;
                            if (string.IsNullOrEmpty(lookUpEditVoucher.EditValue.ToString()) == true)
                            {
                                even.PromotionID = 1;
                            }
                            else
                            {
                                lookUpEditVoucher.Properties.ValueMember = "ID";
                                even.PromotionID = Int32.Parse(lookUpEditVoucher.EditValue.ToString());
                                lookUpEditVoucher.Properties.ValueMember = "PromotionSubject";
                            }
                            if (string.IsNullOrEmpty(lookUpEditGift.EditValue.ToString()) == true)
                            {
                                even.PromotionID2 = 2;
                            }
                            else
                            {
                                lookUpEditGift.Properties.ValueMember = "ID";
                                even.PromotionID2 = Int32.Parse(lookUpEditGift.EditValue.ToString());
                                lookUpEditGift.Properties.ValueMember = "PromotionSubject";
                            }
                            even.Deleted = true;
                            #endregion

                            evensql.Update(even);
                            blEvent.EvenCustomerDeleteByField("EventID", Int32.Parse(txtID.Text));

                        }
                        catch (Exception ex)
                        {
                            checkdel = false;
                            MessageBox.Show(ex.Message);
                        }

                        if (checkdel == true)
                        {
                            MessageBox.Show("Delete Event Successfully!");
                            gridViewSpecialDay.MovePrev();
                            blEvent.RefreshGrid(ListEvent, gridControlSpecialDay);

                        }
                        else
                        {
                            MessageBox.Show("Delete Event Fail!");
                        }
                    }
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            bool check = dxValidationProvider1.Validate();
            if (check)
            {
                if (Adding == true)
                {

                    bool checkadd = true;
                    Event even = new Event();
                    try
                    {
                        #region Mapping field
                        even.Subject = txtDateSubject.Text;
                        even.Description = txtDescription.Text;
                        even.Content = txtSMSFormat.Text;
                        even.EventStatus = (bool)checkEditStatus.EditValue;
                        even.BeginDate = (DateTime)dateEditBegin.EditValue;
                        even.FinishDate = (DateTime)dateEditEnd.EditValue;
                        if (string.IsNullOrEmpty(lookUpEditVoucher.EditValue.ToString()) == true)
                        {
                            even.PromotionID = 1;
                        }
                        else
                        {
                            lookUpEditVoucher.Properties.ValueMember = "ID";
                            even.PromotionID = Int32.Parse(lookUpEditVoucher.EditValue.ToString());
                            lookUpEditVoucher.Properties.ValueMember = "PromotionSubject";
                        }
                        if (string.IsNullOrEmpty(lookUpEditGift.EditValue.ToString()) == true)
                        {
                            even.PromotionID2 = 2;
                        }
                        else
                        {
                            lookUpEditGift.Properties.ValueMember = "ID";
                            even.PromotionID2 = Int32.Parse(lookUpEditGift.EditValue.ToString());
                            lookUpEditGift.Properties.ValueMember = "PromotionSubject";
                        }
                        even.Deleted = false;
                        #endregion

                        evensql.Insert(even);
                        if (ListCustomer.Count == 0)
                        {
                            blEvent.EvenCustomerDeleteByField("EventID", Int32.Parse(txtID.Text));
                        }
                        else
                        {
                            blEvent.EvenCustomerDeleteByField("EventID", Int32.Parse(txtID.Text));
                            blEvent.EventCustomerInsertList(Int32.Parse(txtID.Text), ListCustomer);
                        }
                    }
                    catch (Exception ex)
                    {
                        checkadd = false;
                        MessageBox.Show(ex.Message);
                    }

                    if (checkadd == true)
                    {
                        MessageBox.Show("Add New Event Successfully!");
                        blEvent.RefreshGrid(ListEvent, gridControlSpecialDay);
                        //Turn off mode Adding

                        Adding = false;
                        #region Enable Control

                        gridControlSpecialDay.Enabled = true;
                        btnNew.Enabled = true;
                        btnEdit.Enabled = true;
                        btnPrevious.Enabled = true;
                        btnInsertField.Enabled = false;
                        btnFilter.Enabled = false;
                        btnNext.Enabled = true;
                        btnSave.Enabled = false;
                        btnCancel.Enabled = false;
                        btnDelete.Enabled = true;
                        btnPrevious.Enabled = true;
                        checkEditStatus.Properties.ReadOnly = true;
                        comboBoxEditField.Properties.ReadOnly = true;              
                        txtDateSubject.Properties.ReadOnly = true;
                        txtDescription.Properties.ReadOnly = true;
                        txtGiftDescription.Properties.ReadOnly = true;
                        lookUpEditVoucher.Properties.ReadOnly = true;
                        lookUpEditGift.Properties.ReadOnly = true;
                        txtVoucherDescription.Properties.ReadOnly = true;
                        txtSMSFormat.Properties.ReadOnly = true;
                        dateEditBegin.Properties.ReadOnly = true;
                        dateEditEnd.Properties.ReadOnly = true;
                        gridControlSpecialDay.Enabled = true;

                        #endregion
                        blEvent.RefreshGrid(ListEvent, gridControlSpecialDay);
                    }
                    else
                    {
                        MessageBox.Show("Add New Event Fail!");
                    }
                }
                if (Editing == true)
                {
                    Event even = new Event();
                    bool checkedit = true;
                    try
                    {

                        #region Mapping field
                        even.EventID = Int32.Parse(txtID.Text);
                        even.Subject = txtDateSubject.Text;
                        even.Description = txtDescription.Text;
                        even.Content = txtSMSFormat.Text;
                        even.EventStatus = (bool)checkEditStatus.EditValue;
                        even.BeginDate = (DateTime)dateEditBegin.EditValue;
                        even.FinishDate = (DateTime)dateEditEnd.EditValue;
                        if (string.IsNullOrEmpty(lookUpEditVoucher.EditValue.ToString()) == true)
                        {
                            even.PromotionID = 1;
                        }
                        else
                        {
                            lookUpEditVoucher.Properties.ValueMember = "ID";
                            even.PromotionID = Int32.Parse(lookUpEditVoucher.EditValue.ToString());
                            lookUpEditVoucher.Properties.ValueMember = "PromotionSubject";
                        }
                        if (string.IsNullOrEmpty(lookUpEditGift.EditValue.ToString()) == true)
                        {
                            even.PromotionID2 = 2;
                        }
                        else
                        {
                            lookUpEditGift.Properties.ValueMember = "ID";
                            even.PromotionID2 = Int32.Parse(lookUpEditGift.EditValue.ToString());
                            lookUpEditGift.Properties.ValueMember = "PromotionSubject";
                        }
                        even.Deleted = false;
                        #endregion
                        evensql.Update(even);
                        if (ListCustomer.Count == 0)
                        {
                            blEvent.EvenCustomerDeleteByField("EventID", Int32.Parse(txtID.Text));
                            Customer_MessageSql cmsql = new Customer_MessageSql();
                            cmsql.DeleteByField("EventID", even.EventID);
                        }
                        else
                        {
                            blEvent.EvenCustomerDeleteByField("EventID", Int32.Parse(txtID.Text));
                            blEvent.EventCustomerInsertList(Int32.Parse(txtID.Text), ListCustomer);
                            Customer_MessageSql cmsql = new Customer_MessageSql();
                            cmsql.DeleteByField("EventID", even.EventID);
                            blEvent.CreateSMSForEvent(even, lookUpEditVoucher.EditValue.ToString(), lookUpEditGift.EditValue.ToString(), ListCustomer);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    if (checkedit == true)
                    {
                        MessageBox.Show("Edit Event Successfully!");
                       
                        int i = gridViewSpecialDay.GetFocusedDataSourceRowIndex();
                        blEvent.RefreshGrid(ListEvent, gridControlSpecialDay);
                        gridViewSpecialDay.MoveBy(i);
                        //Turn off mode Editing

                        Editing = false;
                        #region Enable Control
                        checkEditStatus.Properties.ReadOnly = true;
                        btnFilter.Enabled = false;
                        btnNew.Enabled = true;
                        btnDelete.Enabled = true;
                        btnEdit.Enabled = true;
                        btnPrevious.Enabled = true;
                        btnNext.Enabled = true;
                        btnSave.Enabled = false;
                        btnCancel.Enabled = false;
                        txtDateSubject.Properties.ReadOnly = true;
                        txtDescription.Properties.ReadOnly = true;
                        txtGiftDescription.Properties.ReadOnly = true;
                        lookUpEditVoucher.Properties.ReadOnly = true;
                        lookUpEditGift.Properties.ReadOnly = true;
                        txtVoucherDescription.Properties.ReadOnly = true;
                        txtSMSFormat.Properties.ReadOnly = true;
                        dateEditBegin.Properties.ReadOnly = true;
                        dateEditEnd.Properties.ReadOnly = true;
                        gridControlSpecialDay.Enabled = true;


                        #endregion
                    }
                    else
                    {
                        MessageBox.Show("Add New Event Fail!");
                    }

                }
            }
            else
            {
                MessageBox.Show("Some field are not allowed null or blank value");
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (gridViewSpecialDay.RowCount != 0)
            {
                Editing = false;
                Adding = false;
                #region Enable Control
                gridControlSpecialDay.Enabled = true;
                btnSave.Enabled = false;
                btnNext.Enabled = true;
                btnPrevious.Enabled = true;
                btnNew.Enabled = true;
                btnCancel.Enabled = false;
                btnDelete.Enabled = true;
                btnEdit.Enabled = true;
                btnFilter.Enabled = false;
                checkEditStatus.Properties.ReadOnly = true;
                comboBoxEditField.Properties.ReadOnly = true;
                txtDateSubject.Properties.ReadOnly = true;
                txtDescription.Properties.ReadOnly = true;
                txtGiftDescription.Properties.ReadOnly = true;
                lookUpEditVoucher.Properties.ReadOnly = true;
                lookUpEditGift.Properties.ReadOnly = true;
                txtVoucherDescription.Properties.ReadOnly = true;
                dateEditBegin.Properties.ReadOnly = true;
                dateEditEnd.Properties.ReadOnly = true;
                btnPrevious.Enabled = true;
                txtSMSFormat.Properties.ReadOnly = true;
                #endregion
                try
                {
                    EventKeys eventkeys = new EventKeys((int)(gridViewSpecialDay.GetFocusedRowCellValue(colID)));

                    Event even = new Event();
                    even = evensql.SelectByPrimaryKey(eventkeys);
                    MappingField(even);


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void btnFilter_Click(object sender, EventArgs e)
        {

            if (frmBirthdayCustomer == null)
            {
                frmBirthdayCustomer = new frmBirthdayCustomer(this.Name, ListCustomer);
                frmBirthdayCustomer.ShowDialog();

            }
            else
            {
                frmBirthdayCustomer.ShowDialog();
            }


        }
        int CheckLast;
        private void btnNext_Click(object sender, EventArgs e)
        {
            checkClick = false;
            gridViewSpecialDay.MoveNext();
            if (gridViewSpecialDay.IsLastRow)
            {
                gridViewSpecialDay.MoveLast();
                CheckLast++;
                if (CheckLast % 2 == 1)
                {
                    gridViewSpecialDay.MoveFirst();
                }
            }
        }
        int checkFirst = 1;
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            checkClick = false;
            gridViewSpecialDay.MovePrev();
            if (gridViewSpecialDay.IsFirstRow)
            {
                gridViewSpecialDay.MoveFirst();
                checkFirst++;
                if (checkFirst % 2 == 1)
                {
                    gridViewSpecialDay.MoveLast();
                }
            }
        }

        private void frmSpecialDay_Load(object sender, EventArgs e)
        {
            ListEvent = blEvent.RefreshGrid(ListEvent, gridControlSpecialDay);
            if (ListEvent.Count != 0)
            {
                Event even = new Event();
                even = blEvent.GetEventByID(Int32.Parse(gridViewSpecialDay.GetFocusedRowCellValue(colID).ToString()));
                MappingField(even);
            }
            
        }

        private void btnInsertField_Click(object sender, EventArgs e)
        {
            //string smsContent;
            //string customername;
            //string companyName;
            //string companyAddress;
            //string dateSubject;
            //string dateStart;
            //string dateEnd;
            //string gift;
            //string voucher;
            if (comboBoxEditField.Text == "Customer Name")
            {
                txtSMSFormat.Text = txtSMSFormat.Text + "{CustomerName}";
            }
            else
                if (comboBoxEditField.Text == "Company Name")
                {
                    txtSMSFormat.Text = txtSMSFormat.Text + "{CompanyName}";
                }
                else
                    if (comboBoxEditField.Text == "Company Address")
                    {
                        txtSMSFormat.Text = txtSMSFormat.Text + "{CompanyAddress}";
                    }
                    else
                        if (comboBoxEditField.Text == "Date Subject")
                        {
                            txtSMSFormat.Text = txtSMSFormat.Text + "{DateSubject}";
                        }
                        else
                            if (comboBoxEditField.Text == "Date Start")
                            {
                                txtSMSFormat.Text = txtSMSFormat.Text + "{DateStart}";
                            }
                            else
                                if (comboBoxEditField.Text == "Date End")
                                {
                                    txtSMSFormat.Text = txtSMSFormat.Text + "{DateEnd}";
                                }
                                else
                                    if (comboBoxEditField.Text == "Voucher")
                                    {
                                        txtSMSFormat.Text = txtSMSFormat.Text + "{Voucher}";
                                    }
                                    else
                                        if (comboBoxEditField.Text == "Gift")
                                        {
                                            txtSMSFormat.Text = txtSMSFormat.Text + "{Gift}";
                                        }

        }

        private void checkEditStatus_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEditStatus.Checked == true)
            {
                labelStatus.Text = "Event is now enable";
            }
            else
            {
                labelStatus.Text = "Event is now disable";
            }
        }

        private void lookUpEditVoucher_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            lookUpEditVoucher.Properties.ValueMember = "ID";
            Promotion pro = new Promotion();
            pro = blEvent.GetPromotionByID(lookUpEditVoucher.EditValue.ToString());
            txtVoucherDescription.Text = pro.PromotionContent;
            lookUpEditVoucher.Properties.ValueMember = "PromotionSubject";


        }

        private void lookUpEditGift_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            lookUpEditGift.Properties.ValueMember = "ID";
            Promotion pro = new Promotion();
            pro = blEvent.GetPromotionByID(lookUpEditGift.EditValue.ToString());
            txtGiftDescription.Text = pro.PromotionContent;
            lookUpEditGift.Properties.ValueMember = "PromotionSubject";

        }

        private void MappingField(Event even)
        {
            #region Mapping field
            txtDateSubject.Text = even.Subject;
            checkEditStatus.EditValue = even.EventStatus;
            if (even.EventStatus == true)
            {
                labelStatus.Text = "This event is now enable";
            }
            else
            {
                labelStatus.Text = "This event is now disable";
            }
            txtID.Text = even.EventID.ToString();
            txtDescription.Text = even.Description;
            txtSMSFormat.Text = even.Content;
            dateEditBegin.EditValue = even.BeginDate;
            dateEditEnd.EditValue = even.FinishDate;
            #region Lay thong tin promotion
            Promotion proVoucher = new Promotion();
            Promotion proGift = new Promotion();
            proVoucher = blEvent.GetPromotionByID(even.PromotionID.ToString());
            proGift = blEvent.GetPromotionByID(even.PromotionID2.ToString());
            lookUpEditVoucher.EditValue = proVoucher.PromotionSubject;
            lookUpEditGift.EditValue = proGift.PromotionSubject;
            txtVoucherDescription.Text = proVoucher.PromotionContent;
            txtGiftDescription.Text = proGift.PromotionContent;

            #endregion
            #endregion
        }

        bool checkClick = false;
        private void gridViewSpecialDay_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            checkClick = true;
            Event even = new Event();
            even = blEvent.GetEventByID(Int32.Parse(gridViewSpecialDay.GetFocusedRowCellValue(colID).ToString()));

            MappingField(even);

            ListCustomer = blEvent.GetListCustomerOfEvent(Int32.Parse(gridViewSpecialDay.GetFocusedRowCellValue(colID).ToString()));
            frmBirthdayCustomer = new frmBirthdayCustomer(this.Name, ListCustomer);

        }

        private void gridViewSpecialDay_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            gridViewSpecialDay_RowClick(sender, e);
        }

        private void gridViewSpecialDay_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (checkClick == false)
            {
                Event even = new Event();
                even = blEvent.GetEventByID(Int32.Parse(gridViewSpecialDay.GetFocusedRowCellValue(colID).ToString()));
                MappingField(even);
                ListCustomer = blEvent.GetListCustomerOfEvent(Int32.Parse(gridViewSpecialDay.GetFocusedRowCellValue(colID).ToString()));
                frmBirthdayCustomer = new frmBirthdayCustomer(this.Name, ListCustomer);
                checkClick = false;
            }
        }

    }
}